// 以下为官方示例
$().ready(function() {
	validateRule();
	// $("#signupForm").validate();
});

function initForm(){	
	getAllRoles();
	//validateRule();
}

function getAllRoles(){
	var url = ip + 'sys/role/list';
	$.ajax({
		cache : true,
		type : "GET",
		headers:window.PRIVATE_HEADERS,
		url : url,
		data : {},// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(roles) {
			if(roles && !roles.code){
				$.each(roles,function(index,role){
					$('#roles_div').append('<input id="'+role.roleId+'_ck" name="roleIds" type="checkbox" value="'+role.roleId+'" />&nbsp;');
					$('#roles_div').append('<label for="'+role.roleId+'_ck">'+role.roleName+'</label>&nbsp;&nbsp;&nbsp;&nbsp;');
				});

				var userRoleIds = $('#userRoleIds').val().split(',');
				$.each(userRoleIds,function(index,roleId){
					if(roleId && $.trim(roleId)!=''){
						$('input[type=checkbox][name=roleIds][value='+roleId+']').attr("checked","checked");
					}
				});
			}
		}
	});
}


$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
function update() {
	$("#roleIds").val(getCheckedRoles());
	$.ajax({
		cache : true,
		type : "POST",
        headers:window.PRIVATE_HEADERS,
		url : window.API_SERVER_URL + "sys/user/update",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg(data.msg);
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.msg(data.msg);
			}

		}
	});

}

//获取选中的角色
function getCheckedRoles() {
	var roleIds = "";
	$("input:checkbox[name=role]:checked").each(function(i) {
		if (0 == i) {
            roleIds = $(this).val();
		} else {
            roleIds += ("," + $(this).val());
		}
	});
	return roleIds;
}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			},
			username : {
				required : true,
				minlength : 2
			},
			email : {
				required : true,
				email : true
			},
			mobile : {
                required : true
            }
		},
		messages : {
			name : {
				required : icon + "请输入姓名"
			},
			username : {
				required : icon + "请输入用户名",
				minlength : icon + "用户名必须两个字符以上"
			},
            mobile : {
                required : icon + "请输入手机号码"
            },
			email : icon + "请输入E-mail"
		}
	})
}

var openDept = function(){
	layer.open({
		type:2,
		title:"选择部门",
		area : [ '300px', '450px' ],
		content:"../../../templates/system/user/deptTree1.html"
	})
}
function loadDept( deptId,deptName){
	$("#deptId").val(deptId);
	$("#deptName").val(deptName);
}


function  selectAreaPopWin(){
    var contentUrl=SERVER_URL+"templates/popwin/areaspopwin.html";
    layer.open({
        type:2,
        title:"选择区域",
        area : [ '500px', '450px' ],
        content:contentUrl,
        success: function(layero, index){
            var body = layer.getChildFrame('body',index);//建立父子联系
            var iframeWin = window[layero.find('iframe')[0]['name']];
            iframeWin.getTreeData(function (earas) {

                var areaIds = '';
                var areaNames = '';
                if(earas){
                    $.each(earas,function (ai,eara) {
                        if(eara.id!=-1){//排除手动创建的根节点
                            if(areaIds!=''){
                                areaIds += ',';
                                areaNames += ',';
                            }
                            areaIds += eara.id;
                            areaNames += eara.text;
                        }
                    });
                }
                $('#areaIds').val(areaIds);
                $('#areaNames').val(areaNames);
                layer.close(index);
            },$('#areaIds').val());
        }
    })
}